package org.jeecg.modules.flow.callback.outbound.task;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.business.doc.outbound.entity.WmsOutReceipt;
import org.jeecg.modules.business.outbound.entity.WmsOutReceiptTask;
import org.jeecg.modules.business.outbound.service.IWmsOutReceiptTaskService;
import org.jeecg.modules.business.third.service.ISapOperateService;
import org.jeecg.modules.dock.entity.dto.response.SellOutboundResponseDTO;
import org.jeecg.modules.flow.callback.enums.DocumentStatusEnum;
import org.jeecg.modules.flow.callback.enums.OutboundStatusEnum;
import org.jeecg.modules.flow.callback.enums.SimpleDocumentTypeEnum;
import org.jeecg.modules.flow.callback.enums.TransferStatusEnum;
import org.jeecg.modules.flow.sys.common.BaseCustomBeanRun;
import org.jeecg.modules.flow.sys.entity.dto.BaseFlowDto;
import org.jeecg.modules.flow.sys.exception.FlowCurrentFailException;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 销售出库过账节点 回调bean
 *
 * @author gongwenkai
 * @version 1.0.0
 * @since 2023-09-26
 */
@Slf4j
@RequiredArgsConstructor
@Component
public class OutBoundTaskXSCKTransferBean<T> extends BaseCustomBeanRun<T> {

    private final IWmsOutReceiptTaskService wmsOutReceiptTaskService;

    private final ISapOperateService sapOperateService;

    /**
     * 业务逻辑 销售出库过账节点
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void actureRun() {
        BaseFlowDto baseFlowDto = this.getBaseFlowDto();
        String taskId = baseFlowDto.getInfoId();
        WmsOutReceiptTask wmsOutReceiptTask = wmsOutReceiptTaskService.getById(taskId);
        if(Objects.isNull(wmsOutReceiptTask)){
            throw new JeecgBootException("出库任务不存在");
        }
        if (DocumentStatusEnum.CLOSE.getCode().equals(wmsOutReceiptTask.getDocumentStatus())) {
            throw new JeecgBootException("提交失败，出库任务已关闭");
        }
        //如果全部出库了，去过账
        if(OutboundStatusEnum.ALL.getCode().equals(wmsOutReceiptTask.getOutboundStatus())){
            Boolean successFlag=true;
            //调用第三方接口
            List<SellOutboundResponseDTO> sellOutboundResponseDTOList=new ArrayList<>();
            try{
                sellOutboundResponseDTOList = sapOperateService.outboundByTask(taskId);
            }catch (Exception e){
                log.error(e.getMessage());
                successFlag=false;
                baseFlowDto.setResult(Result.error(e.getMessage()));
                //更新过账状态 过账失败
                wmsOutReceiptTaskService.updateTransferStatus(SimpleDocumentTypeEnum.OUT_TASK,taskId,null, TransferStatusEnum.TRANSFER_FAIL);
            }

            if(!CollectionUtil.isEmpty(sellOutboundResponseDTOList)){
                for(SellOutboundResponseDTO sellOutboundResponseDTO:sellOutboundResponseDTOList){
                    if("E".equals(sellOutboundResponseDTO.getResult())){
                        successFlag=false;
                        baseFlowDto.setResult(Result.error(sellOutboundResponseDTO.getMessage()));
                        //更新过账状态 过账失败
                        wmsOutReceiptTaskService.updateTransferStatus(SimpleDocumentTypeEnum.OUT_TASK,taskId,null, TransferStatusEnum.TRANSFER_FAIL);
                    }
                }
            }
            if(successFlag){
                //更新过账状态 过账成功
                wmsOutReceiptTaskService.updateTransferStatus(SimpleDocumentTypeEnum.OUT_TASK,taskId,null, TransferStatusEnum.TRANSFER_SUCCESS);
                // 当前前端调用业务需要你返回的
                baseFlowDto.setResult(Result.OK());
            }
        }
        // id是保存业务记录的id。用于生成业务流程信息记录
        baseFlowDto.setInfoId(taskId);
    }


}
